package com.lxw.service;

import com.lxw.dao.CmsPageRepository;
import com.lxw.dao.CmsSiteRepository;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsSite;
import com.lxw.framework.domain.cms.response.CmsCode;
import com.lxw.framework.exception.ExceptionCast;
import com.lxw.framework.model.response.CommonCode;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

/**
 *
 */
@Service
@Slf4j
public class CmsPagePostService {
    @Autowired
    private CmsPageRepository cmsPageRepository;

    @Autowired
    private CmsSiteRepository cmsSiteRepository;

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFSBucket gridFSBucket;

    //把gridFs上的页面的html页面放到指定的目录
    public void savePage2Location(String pageId){
        //根据pageId获取cmsPage
        Optional<CmsPage> optional = cmsPageRepository.findById(pageId);
        if(!optional.isPresent()){
            ExceptionCast.cast(CmsCode.CMS_GET_PAGE_NULL_ERROR);
        }
        CmsPage cmsPage = optional.get();
        String pagePhysicalPath = cmsPage.getPagePhysicalPath();
        String pageName = cmsPage.getPageName();

        String siteId = cmsPage.getSiteId();
        CmsSite cmsSite = this.getCmsSiteById(siteId);
        String sitePhysicalPath = cmsSite.getSitePhysicalPath();
        //拼接物理路径
        String pagePath = sitePhysicalPath+pagePhysicalPath+pageName;
        //根据htmlFileId获取页面html内容
        String htmlFileId = cmsPage.getHtmlFileId();
        try {
            InputStream inputStream = this.getHtmlFileById(htmlFileId);
            FileOutputStream fileOutputStream = new FileOutputStream(pagePath);
            IOUtils.copy(inputStream,fileOutputStream);
        } catch (IOException e) {
            log.error("CmsPagePostService savePage2Location"+e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 根据htmlFileId获取页面html内容
     */

    public InputStream getHtmlFileById(String htmlFileId){

//        return new FileUtils().getFileByIdMongDb(htmlFileId);
        try{
            GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(htmlFileId)));
            GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
            GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
            return gridFsResource.getInputStream();
        }catch (IOException e){
            log.error("CmsPagePostService  getHtmlFileById"+e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据站点id得到站点
     */
    public CmsSite getCmsSiteById(String siteId){
        Optional<CmsSite> optionalCmsSite = cmsSiteRepository.findById(siteId);
        if (!optionalCmsSite.isPresent()){
            ExceptionCast.cast(CommonCode.FAIL);
        }
        CmsSite cmsSite = optionalCmsSite.get();
        return cmsSite;
    }

}
